package com.wash.shoes.controller.pc;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.domain.R;
import com.wash.shoes.entity.OrderGoodPo;
import com.wash.shoes.entity.OrderPo;
import com.wash.shoes.entity.WorkOrderNodePo;
import com.wash.shoes.entity.WorkOrderPo;
import com.wash.shoes.request.CreateWorkOrderRequest;
import com.wash.shoes.request.GetWorkOrderDetailRequest;
import com.wash.shoes.request.WorkOrderQueryRequest;
import com.wash.shoes.request.WorkOrderStatusRequest;
import com.wash.shoes.resp.WorkOrderDetailResp;
import com.wash.shoes.service.IWorkOrderNodeService;
import com.wash.shoes.service.IWorkOrderService;
import com.wash.shoes.service.OrderGoodService;
import com.wash.shoes.service.OrderService;
import com.wash.shoes.util.AdminUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
@Api(tags = {"PC-工单"})
@RequestMapping({"/pc/workOrder", "workOrder"})
@RequiredArgsConstructor
public class WorkOrderController {
    /**
     * 工单服务
     */
    private final IWorkOrderService workOrderService;
    /**
     * 工单节点服务
     */
    private final IWorkOrderNodeService workOrderNodeService;
    //订单商品服务
    private final OrderGoodService orderGoodService;
    //节点初始状态
    private static final String INIT_STATUS = "0";
    //节点完结状态
    private static final String FINISH_STATUS = "1";
    //订单服务
    private final OrderService orderService;


    @ApiOperation("创建工单和增加节点")
    @PostMapping("/create")
    @Transactional
    public R<?> createWorkOrder(@RequestBody @Valid CreateWorkOrderRequest request) {

        WorkOrderPo workOrderPo = workOrderService.getOne(new LambdaQueryWrapper<WorkOrderPo>().eq(WorkOrderPo::getOrderId, request.getOrderId()).eq(WorkOrderPo::getStatus, INIT_STATUS).orderByDesc(WorkOrderPo::getCreateTime).last("limit 1"));
        if (Objects.isNull(workOrderPo)) {
            // 创建工单
            workOrderPo = new WorkOrderPo();
            OrderPo orderPo = orderService.getById(request.getOrderId());
            Assert.notNull(orderPo, "订单不存在");
            List<OrderGoodPo> orderGoodPoList = orderGoodService.list(new LambdaQueryWrapper<OrderGoodPo>().eq(OrderGoodPo::getOrderId, orderPo.getId()));
            Assert.notNull(orderGoodPoList, "订单商品不存在");
            workOrderPo.setOrderId(orderPo.getId());
            workOrderPo.setOrderNo(orderPo.getOrderNo());
            workOrderPo.setWashFactory(orderPo.getWashFactory());
            workOrderPo.setFactoryId(orderPo.getFactoryId());
            workOrderPo.setShopId(orderPo.getShopId());
            workOrderPo.setWashShop(orderPo.getWashShop());
            workOrderPo.setCustomPhone(orderPo.getCustomPhone());
            workOrderPo.setStatus(INIT_STATUS);
            workOrderPo.setComeFrom(orderPo.getComeFrom());
            workOrderPo.setTotalPrice(orderPo.getTotalPrice());
            workOrderPo.setMtBeginTime(orderPo.getMtBeginTime());
            workOrderPo.setCreateBy(AdminUserUtil.getLoginUserName());
            workOrderPo.setUpdateBy(AdminUserUtil.getLoginUserName());
            workOrderPo.setGoodsName(orderGoodPoList.get(0).getName());
            workOrderService.save(workOrderPo);
        }
        WorkOrderNodePo workOrderNodePo = new WorkOrderNodePo();
        workOrderNodePo.setWorkOrderId(workOrderPo.getId());
        workOrderNodePo.setContent(request.getContent());
        workOrderNodePo.setOrderId(request.getOrderId());
        workOrderNodePo.setStatus(INIT_STATUS);
        workOrderNodePo.setCreateBy(AdminUserUtil.getLoginUserName());
        workOrderNodePo.setUpdateBy(AdminUserUtil.getLoginUserName());
        workOrderNodePo.setCreateTime(new Date());
        workOrderNodePo.setUpdateTime(new Date());
        //创建工单节点
        workOrderNodeService.save(workOrderNodePo);
        return R.ok();
    }

    @PostMapping("/list")
    @ApiOperation("工单列表")
    public R<IPage<WorkOrderPo>> list(@RequestBody WorkOrderQueryRequest request) {
      /*  workOrderService.list(request.toPage(), new LambdaQueryWrapper<WorkOrderPo>()
                .likeRight(Objects.nonNull(request.getKeyword()), WorkOrderPo::getOrderNo, request.getKeyword()).or().likeRight(Objects.nonNull(request.getKeyword()), WorkOrderPo::getCustomPhone, request.getKeyword()));*/

        return R.ok(workOrderService.page(request.toPage(), request.getOrderNo(), request.getPhone()));
    }

    @GetMapping("/detail")
    @ApiOperation(value = "工单详情", response = WorkOrderDetailResp.class)
    public R<?> getWorkOrderDetail(@Valid GetWorkOrderDetailRequest request) {
        WorkOrderPo workOrderPo = workOrderService.getById(request.getWorkOrderId());
        if (Objects.isNull(workOrderPo)) {
            return R.fail("工单不存在");
        }
        List<WorkOrderNodePo> data = workOrderNodeService.list(new LambdaQueryWrapper<WorkOrderNodePo>().eq(WorkOrderNodePo::getWorkOrderId, request.getWorkOrderId()).orderByDesc(WorkOrderNodePo::getCreateTime));
        return R.ok(WorkOrderDetailResp.builder().workOrder(workOrderPo).workOrderNode(data).build());
    }

    @PostMapping("/updateStatus")
    @ApiOperation("修改工单状态完结")
    public R<?> updateWorkOrderStatus(@RequestBody @Valid WorkOrderStatusRequest workOrderStatusRequest) {
        workOrderService.update(new LambdaUpdateWrapper<WorkOrderPo>().eq(WorkOrderPo::getId, workOrderStatusRequest.getWorkOrderId())
                .set(WorkOrderPo::getStatus, FINISH_STATUS));
        workOrderNodeService.update(new LambdaUpdateWrapper<WorkOrderNodePo>().eq(WorkOrderNodePo::getWorkOrderId, workOrderStatusRequest.getWorkOrderId())
                .set(WorkOrderNodePo::getStatus, FINISH_STATUS));
        return R.ok();
    }
}